package com.gewu.pm.ui.activity.terminal;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.ui.adapter.promotion.InfoItemAdapter;
import com.gewu.pm.ui.adapter.promotion.PromotionProductAdapter;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 终端促销活动详情
 * <p>
 * type=1: 买赠活动（显示促销主品+促销赠品）
 * type=2: 满减活动（显示促销主品）
 * type=3: 优惠券活动（显示使用规则）
 */
public final class EventDetailsActivity extends AppActivity
        implements BaseAdapter.OnItemClickListener {

    /**
     * 1.买赠活动  2.满减活动 3.优惠券活动
     */
    private int mType;

    private TextView mStatusView;
    private RecyclerView mBasicInfoRecyclerView;
    private RecyclerView mCustomerScopeRecyclerView;
    private RecyclerView mPromotionRulesRecyclerView;
    private LinearLayout mLayoutMainProducts;
    private RecyclerView mMainProductsRecyclerView;
    private LinearLayout mLayoutGiftProducts;
    private RecyclerView mGiftProductsRecyclerView;
    private LinearLayout mLayoutUsageRules;
    private RecyclerView mUsageRulesRecyclerView;

    private InfoItemAdapter mBasicInfoAdapter;
    private InfoItemAdapter mCustomerScopeAdapter;
    private InfoItemAdapter mPromotionRulesAdapter;
    private PromotionProductAdapter mMainProductsAdapter;
    private PromotionProductAdapter mGiftProductsAdapter;
    private InfoItemAdapter mUsageRulesAdapter;

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, EventDetailsActivity.class);
        intent.putExtra(IntentKey.TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_event_details;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mStatusView = findViewById(R.id.tv_status);

        // 基本信息
        mBasicInfoRecyclerView = findViewById(R.id.rv_basic_info);
        mBasicInfoRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBasicInfoAdapter = new InfoItemAdapter(this);
        mBasicInfoRecyclerView.setAdapter(mBasicInfoAdapter);

        // 客户范围
        mCustomerScopeRecyclerView = findViewById(R.id.rv_customer_scope);
        mCustomerScopeRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mCustomerScopeAdapter = new InfoItemAdapter(this);
        mCustomerScopeRecyclerView.setAdapter(mCustomerScopeAdapter);

        // 促销规则
        mPromotionRulesRecyclerView = findViewById(R.id.rv_promotion_rules);
        mPromotionRulesRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mPromotionRulesAdapter = new InfoItemAdapter(this, getInt(IntentKey.TYPE) == 3 ? 111 : 0);//只有=3.优惠券活动是时候传111
        mPromotionRulesRecyclerView.setAdapter(mPromotionRulesAdapter);

        // 促销主品
        mLayoutMainProducts = findViewById(R.id.layout_main_products);
        mMainProductsRecyclerView = findViewById(R.id.rv_main_products);
        mMainProductsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mMainProductsAdapter = new PromotionProductAdapter(this);
        mMainProductsAdapter.setOnItemClickListener(this);
        mMainProductsRecyclerView.setAdapter(mMainProductsAdapter);

        // 促销赠品
        mLayoutGiftProducts = findViewById(R.id.layout_gift_products);
        mGiftProductsRecyclerView = findViewById(R.id.rv_gift_products);
        mGiftProductsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mGiftProductsAdapter = new PromotionProductAdapter(this);
        mGiftProductsAdapter.setOnItemClickListener(this);
        mGiftProductsRecyclerView.setAdapter(mGiftProductsAdapter);

        // 使用规则
        mLayoutUsageRules = findViewById(R.id.layout_usage_rules);
        mUsageRulesRecyclerView = findViewById(R.id.rv_usage_rules);
        mUsageRulesRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mUsageRulesAdapter = new InfoItemAdapter(this);
        mUsageRulesRecyclerView.setAdapter(mUsageRulesAdapter);
    }

    @Override
    protected void initData() {
        mType = getInt(IntentKey.TYPE, 1);

        // 设置标题
        setTitle(getTitleByType());

        // 根据 type 显示不同内容
        updateViewByType();

        // 加载数据
        loadMockData();
    }

    /**
     * 根据 type 获取标题
     */
    private String getTitleByType() {
        switch (mType) {
            case 1:
                return "买赠活动详情";
            case 2:
                return "满减活动详情";
            case 3:
                return "优惠券活动详情";
            default:
                return "促销活动详情";
        }
    }

    /**
     * 根据 type 显示不同内容
     */
    private void updateViewByType() {
        if (mType == 1) {
            // 买赠活动：显示促销主品和促销赠品
            mLayoutMainProducts.setVisibility(View.VISIBLE);
            mLayoutGiftProducts.setVisibility(View.VISIBLE);
            mLayoutUsageRules.setVisibility(View.GONE);
        } else if (mType == 2) {
            // 满减活动：只显示促销主品
            mLayoutMainProducts.setVisibility(View.VISIBLE);
            mLayoutGiftProducts.setVisibility(View.GONE);
            mLayoutUsageRules.setVisibility(View.GONE);
        } else if (mType == 3) {
            // 优惠券活动：显示使用规则
            mLayoutMainProducts.setVisibility(View.GONE);
            mLayoutGiftProducts.setVisibility(View.GONE);
            mLayoutUsageRules.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 加载模拟数据
     */
    private void loadMockData() {
        // 基本信息
        List<String[]> basicInfo = new ArrayList<>();
        if (mType == 1) {
            basicInfo.add(new String[]{"促销活动名称", "买2盒豆腐王送1盒内酯豆腐"});
            basicInfo.add(new String[]{"活动详情", "年中促销，惊喜多多"});
            basicInfo.add(new String[]{"订单日期", "2025-08-11~2025-08-18"});
            basicInfo.add(new String[]{"活动类型", "区域活动"});
        } else if (mType == 2) {
            basicInfo.add(new String[]{"促销活动名称", "满20元减2元"});
            basicInfo.add(new String[]{"活动详情", "年中促销，惊喜多多"});
            basicInfo.add(new String[]{"订单日期", "2025-08-11~2025-08-18"});
            basicInfo.add(new String[]{"活动类型", "公司活动"});
        } else if (mType == 3) {
            basicInfo.add(new String[]{"促销活动名称", "5元优惠券"});
            basicInfo.add(new String[]{"活动详情", "年中促销，惊喜多多"});
            basicInfo.add(new String[]{"订单日期", "2025-08-11~2025-08-18"});
            basicInfo.add(new String[]{"发放时间", "2025-08-11 12:00:00"});
            basicInfo.add(new String[]{"活动类型", "公司活动"});
        }
        mBasicInfoAdapter.setData(basicInfo);

        // 客户范围
        List<String[]> customerScope = new ArrayList<>();
        customerScope.add(new String[]{"促销对象", "终端"});
        customerScope.add(new String[]{"客户范围", "终端-7654a-1、终端-9877a-1"});
        mCustomerScopeAdapter.setData(customerScope);

        // 促销规则
        List<String[]> promotionRules = new ArrayList<>();
        if (mType == 1) {
            promotionRules.add(new String[]{"促销类型", "主品达到满赠数量,送固定数量赠品"});
            promotionRules.add(new String[]{"赠送方式", "赠送全部"});
            promotionRules.add(new String[]{"单笔订单主品订购数量满 5 盒，即送出全部赠品", ""});
            promotionRules.add(new String[]{"满送规则", "每买X送Y"});
        } else if (mType == 2) {
            promotionRules.add(new String[]{"促销类型", "单笔订单达到满减金额,优惠部分金额"});
            promotionRules.add(new String[]{"赠送方式", "循环满减"});
            promotionRules.add(new String[]{"促销主品金额每满 20 元，即减少 2 元", ""});
        } else if (mType == 3) {
            promotionRules.add(new String[]{"优惠券策略", "按策略发放"});
            promotionRules.add(new String[]{"自主下单策略配置", "终端用户自主下单达到2天"});
            promotionRules.add(new String[]{"发放设置", "活动期间只发放2天"});
        }
        mPromotionRulesAdapter.setData(promotionRules);

        // 促销主品（type=1,2）
        if (mType == 1 || mType == 2) {
            List<String> products = new ArrayList<>();
            products.add("商品1");
            mMainProductsAdapter.setData(products);
        }

        // 促销赠品（type=1）
        if (mType == 1) {
            List<String> gifts = new ArrayList<>();
            gifts.add("赠品1");
            mGiftProductsAdapter.setData(gifts);
        }

        // 使用规则（type=3）
        if (mType == 3) {
            List<String[]> usageRules = new ArrayList<>();
            usageRules.add(new String[]{"优惠券类型", "有门槛/无门槛"});
            usageRules.add(new String[]{"门槛金额", "满50可用/——"});
            usageRules.add(new String[]{"优惠券金额", "5"});
            usageRules.add(new String[]{"有效期", "发放后7天内有效"});
            mUsageRulesAdapter.setData(usageRules);
        }
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        toast("点击商品");
        // TODO: 打开商品详情
    }
}